package nix.hibernate;

import java.util.Arrays;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

public class DemoSession {

	/**
	 * 添加
	 */
	@Test
	public void fun1() {
		// 读取配置文件（规范命名可使用无参方法，也可指定配置文件）
		Configuration conf = new Configuration().configure();
		// 创建Factory
		SessionFactory sessionFactory = conf.buildSessionFactory();
		// 获得session对象
		Session session = sessionFactory.openSession();
		// 操作数据库
		User user = new User();
		user.setName("lisi");
		user.setPassword("123");
		session.save(user);
		// 关闭资源
		session.close();
		sessionFactory.close();
	}
	
	/**
	 * 修改
	 */
	@Test
	public void fun2() {
		Configuration conf = new Configuration().configure();
		SessionFactory sessionFactory = conf.buildSessionFactory();
		Session session = sessionFactory.openSession();
		// 开启事务
		Transaction ts = session.beginTransaction();
		// 查询
		User user = (User) session.get(User.class, 1);
		System.out.println(user);
		// 修改
		user.setName("zhangSan");
		session.update(user);
		// 提交事务
		ts.commit();
		// 关闭资源
		session.close();
		sessionFactory.close();		
	}
	
	/**
	 * 删除
	 */
	@Test
	public void fun3() {
		Configuration conf = new Configuration().configure();
		SessionFactory sessionFactory = conf.buildSessionFactory();
		Session session = sessionFactory.openSession();
		// 开启事务
		Transaction ts = session.beginTransaction();
		// 指定删除主键
		// User user = (User) session.get(User.class, 2);
		User user = new User();
		user.setId(2);
		// 删除
		session.delete(user);
		// 提交事务
		ts.commit();
		// 关闭资源
		session.close();
		sessionFactory.close();
	}
	
	/**
	 * get查询
	 */
	@Test
	public void fun4() {
		Configuration conf = new Configuration().configure();
		SessionFactory sessionFactory = conf.buildSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction ts = session.beginTransaction();
		// get执行时，立即查询数据库
		User user = (User) session.get(User.class, 1);
		ts.commit();
		// 关闭资源
		session.close();
		sessionFactory.close();
		System.out.println(user);
	}
	
	/**
	 * load查询
	 */
	@Test
	public void fun5() {
		Configuration conf = new Configuration().configure();
		SessionFactory sessionFactory = conf.buildSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction ts = session.beginTransaction();
		// load执行时，暂不执行查询，使用该对象（结果）时再执行查询
		User user = (User) session.load(User.class, 1);
		ts.commit();
		// 关闭资源
		session.close();
		sessionFactory.close();
		System.out.println(user);
	}
	
	/**
	 * 查询所有HQL
	 */
	@Test
	public void fun6() {
		Configuration conf = new Configuration().configure();
		SessionFactory sessionFactory = conf.buildSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction ts = session.beginTransaction();
		// 使用HQL查询
		Query query = session.createQuery("from nix.hibernate.User");
		List<User> userList = query.list();
		ts.commit();
		// 关闭资源
		session.close();
		sessionFactory.close();
		System.out.println(userList);
	}
	
	/**
	 * 查询所有Criteria
	 */
	@Test
	public void fun7() {
		Configuration conf = new Configuration().configure();
		SessionFactory sessionFactory = conf.buildSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction ts = session.beginTransaction();
		// 使用Criteria查询
		Criteria criteria = session.createCriteria(User.class);
		List<User> userList = criteria.list();
		ts.commit();
		// 关闭资源
		session.close();
		sessionFactory.close();
		System.out.println(userList);
	}
	
	/**
	 * 查询所有SQL
	 */
	@Test
	public void fun8() {
		Configuration conf = new Configuration().configure();
		SessionFactory sessionFactory = conf.buildSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction ts = session.beginTransaction();
		// 使用SQL查询
		SQLQuery query = session.createSQLQuery("select * from t_user");
		// 查询默认返回Object数组的集合
		List<Object[]> list = query.list();
		for (Object[] objects : list) {
			System.out.print(Arrays.toString(objects));
			// [1, zhangSan, 123][3, lisi, 123]
		}
		// 设置实体类型，直接实现Mapping
		query.addEntity(User.class);
		List<User> userList = query.list();
		System.out.println(userList);
		ts.commit();
		// 关闭资源
		session.close();
		sessionFactory.close();
	}
}
